/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AddTareaDialog.java
 *
 * Created on 02-may-2011, 19:44:47
 */

package ia_psr.transportesGUI;

/**
 *
 * @author Rubén Avalos Elvira y Fabiana Celiberti Tapie
 */
import ia_psr.builder.GraphBuilder;
import ia_psr.builder.GrafoEmpresaTransportesBuilder;
import ia_psr.graph.Tarea;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class AddTareaDialog extends javax.swing.JFrame {
    GraphBuilder builder;
    JPanel parent;

    /** Creates new form AddTareaDialog */
    public AddTareaDialog(JPanel parent) {
        initComponents();
        this.parent = parent;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnAddTarea = new javax.swing.JButton();
        jSeparator2 = new javax.swing.JSeparator();
        txtTareaName = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtHoraFin = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtHoraInicio = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        txtDuracion = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        cbTipo = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        btnCancelar = new javax.swing.JButton();

        setTitle("ETaskSkeduler - Añadir Tarea");

        btnAddTarea.setText("Añadir");
        btnAddTarea.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                btnAddTareaMouseReleased(evt);
            }
        });

        jLabel1.setText("Tipo:");

        jLabel2.setText("Datos de la Tarea");

        jLabel3.setText("Nombre:");

        jLabel6.setText("Duración:");

        jLabel5.setText("Hora Fin:");

        cbTipo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Programada", "No Programada" }));

        jLabel4.setText("Hora Inicio:");

        btnCancelar.setText("Cancelar");
        btnCancelar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                btnCancelarMouseReleased(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jSeparator2, javax.swing.GroupLayout.DEFAULT_SIZE, 549, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel1)
                                    .addComponent(jLabel3))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(txtTareaName)
                                    .addComponent(cbTipo, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(28, 28, 28)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel4)
                                    .addComponent(jLabel6))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(txtDuracion, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(txtHoraInicio, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 87, Short.MAX_VALUE))
                                .addGap(24, 24, 24)
                                .addComponent(jLabel5)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtHoraFin, javax.swing.GroupLayout.DEFAULT_SIZE, 89, Short.MAX_VALUE))))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(403, 403, 403)
                        .addComponent(btnAddTarea, javax.swing.GroupLayout.DEFAULT_SIZE, 75, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnCancelar)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtTareaName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4)
                    .addComponent(txtHoraInicio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(txtHoraFin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(8, 8, 8)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cbTipo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(txtDuracion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addGap(7, 7, 7)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCancelar)
                    .addComponent(btnAddTarea))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCancelarMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnCancelarMouseReleased
        // TODO add your handling code here:
        this.setVisible(false);
    }//GEN-LAST:event_btnCancelarMouseReleased

    private void btnAddTareaMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnAddTareaMouseReleased
        // TODO add your handling code here:
        try {
            if (txtTareaName.getText() != null) {
                if (cbTipo.getSelectedIndex() == 0) { // Programable
                    if (txtHoraInicio.getText() == null || txtHoraFin.getText() == null) {
                        throw new Exception();
                    }
                } else {
                    if (txtDuracion.getText() == null) {
                        throw new Exception();
                    }
                }
                // añadir tarea
                String nombre = txtTareaName.getText();
                int tipo = cbTipo.getSelectedIndex();
                Tarea t;
                if (tipo == 0) {
                    int horaInicio = Integer.parseInt(txtHoraInicio.getText());
                    int horaFin = Integer.parseInt(txtHoraFin.getText());
                    t = new Tarea(nombre, horaInicio, 0, horaFin, 0);
                } else {
                    int duracion = Integer.parseInt(txtDuracion.getText());
                    t = new Tarea(nombre, duracion);
                }
                builder = GrafoEmpresaTransportesBuilder.getInstance();
                builder.buildNode(t);

                ((TareasPanel)parent).populateTareasTable();

                setVisible(false);
            }
        } catch (Exception e) {
            // mostrar error dialog
            JFrame frame = new JFrame();
            JOptionPane.showMessageDialog(frame,
                    "Faltan datos de tarea o éstos no son válidos",
                    "Atención",
                    JOptionPane.WARNING_MESSAGE);
        }

    }//GEN-LAST:event_btnAddTareaMouseReleased

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAddTarea;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JComboBox cbTipo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JTextField txtDuracion;
    private javax.swing.JTextField txtHoraFin;
    private javax.swing.JTextField txtHoraInicio;
    private javax.swing.JTextField txtTareaName;
    // End of variables declaration//GEN-END:variables

}
